/*
 * Daniel R Padilla
 *
 * Copyright (c) 2007, Daniel R Padilla
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package org.databasefacade.table.column;

import org.databasefacade.data.RenderingMetaData;
import org.databasefacade.table.Table;
import org.databasefacade.table.column.datatype.DataType;
import org.databasefacade.translator.Translator;


/**
 * 
 * The class holds three critical pieces of information.
 * The first is the database meta-data which the library
 * uses to create, select, update, insert and delete from
 * tables.
 * 
 */
public class Column {
	private ColumnMetaData colDatabaseMetaData;
	private RenderingMetaData colRenderingMetaData = new RenderingMetaData();
	private String tableName;
	private String tableAlias;
	private String columnName;
	private Translator translator;

	
	/**
	 * Used to create copy internally not intended for
	 * external use. Also, translator is very critical
	 * that we get it it from the outside world
	 */
	private Column() {
		
	}
	  
    public Column(Table table, String colName, DataType type) {
    	init(table,colName,type,false,null,null);
    	this.translator = table.getDBHelper().getTranslator();
    }
        
    private void init(
    		Table table, 
    		String colName, 
    		DataType type, 
    		boolean isPrimaryKey,
    		String foreignTableName,
    		String foreignColumnName) {
    	setTableName(table.getTableName());
    	tableAlias = table.getTableAlias();
    	columnName = colName;
    	colDatabaseMetaData = new ColumnMetaData(table, colName, type);
    	colDatabaseMetaData.setColumnName(colName);
    }
    
    public Translator getTranslator() {
    	return translator;
    }
    
    public void setTableName(String tableName) {
    	this.tableName=tableName;
    }
    public String getTableName() {
    	return tableName;
    }
    
    public void setTableAlias(String tableAlias) {
    	this.tableAlias=tableAlias;
    	colDatabaseMetaData.setTableAlias(tableAlias);
    }
    public String getTableAlias() {
    	return tableAlias;
    }
    
    
    public void setColumnName(String colName) {
    	columnName=colName;
    }
    public String getColumnName() {
    	return columnName;
    }
    
    public void setDatabaseMetaData(ColumnMetaData meta) {
    	colDatabaseMetaData=meta;
    }
    public ColumnMetaData getDatabaseMetaData() {
    	return colDatabaseMetaData;
    }
    
    public void setRenderingMetaData(RenderingMetaData meta) {
    	colRenderingMetaData=meta;
    }
    public RenderingMetaData getRenderingMetaData() {
    	return colRenderingMetaData;
    }
    
    public String getStringDelimiter() {
    	return translator.getStringDelimiter();
    }
           
    public Column getCopy() {
    	Column col = new Column();
    	col.setDatabaseMetaData(colDatabaseMetaData.getCopy());
    	col.setRenderingMetaData(colRenderingMetaData.getCopy());
    	col.setTableName(new String(getTableName()));
    	col.setTableAlias(new String(getTableAlias()));
    	col.setColumnName(new String(getColumnName()));
    	col.translator = translator;
    	return col;
    }
    
    public String toString() {
    	return columnName;
    }
}
